package example.trace.opentelemetry;

import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.metrics.LongCounter;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.Tracer;

public final class LoggingExporterExample {

    private static final String INSTRUMENTATION_NAME = LoggingExporterExample.class.getName();

    private final Tracer tracer;
    private final LongCounter counter;

    public LoggingExporterExample(OpenTelemetry openTelemetry) {
        tracer = openTelemetry.getTracer(INSTRUMENTATION_NAME);
        counter = openTelemetry.getMeter(INSTRUMENTATION_NAME).counterBuilder("work_done").build();
    }

    public void myWonderfulUseCase() {
        Span span = this.tracer.spanBuilder("start my wonderful use case").startSpan();
        span.addEvent("Event 0");
        doWork();
        span.addEvent("Event 1");
        span.end();
    }

    private void doWork() {
        Span span = this.tracer.spanBuilder("doWork").startSpan();
        try {
            Thread.sleep(1000);
            counter.add(1);
        } catch (InterruptedException e) {
            // do the right thing here
        } finally {
            span.end();
        }
    }

    public static void main(String[] args) {
        // it is important to initialize your SDK as early as possible in your application's lifecycle
        OpenTelemetry openTelemetry = LoggingOpenTelemetrySupport.getOpenTelemetry();

        // Start the example
        LoggingExporterExample example = new LoggingExporterExample(openTelemetry);
        // Generate a few sample spans
        for (int i = 0; i < 5; i++) {
            example.myWonderfulUseCase();
        }

        try {
            // Flush out the metrics that have not yet been exported
            Thread.sleep(1000L);
        } catch (InterruptedException e) {
            // ignore since we're exiting
        }

        System.out.println("Bye");
    }

}
